﻿var div_Widgets = null;
var div_TabList = null;
var div_tabHolder = null;
var div_tabLastElement = null;
var div_WidgetProgress = null;
var div_Messages = null;
var a_Messages = null;
var Menu = null;

function ShowPageHint(str, type)
{
   if (type == 0)
   {
      $hide('div_hint');
   }
   else
   {
      var obj = "";
      switch (type)
      {
         case 1:
            obj += "<img src='/images/hint.png' border='0' />";
            break;
      }
      
      obj += str;
      $show('div_hint');
      $('div_hint').innerHTML = obj;
   }
}

function ShowNoWidget()
{
   div_NoWidgets.style.display = "";
}

function HideNoWidget()
{
   div_NoWidgets.style.display = "none";
}

function setDebug(intVal)
{
   if (intVal == 1)
   {
      $show('debug');
   }
   else
   {
      $hide('debug');
   }
}

var Msg =
{
    InitTicker: function() {
        if (Msg.Ticker) {
        }
        else {
            Msg.Ticker = "Ticker";
            Msg.TickerEnabled = true;
            Msg.TickerString = "";
        }
    },

    ToggleTicker: function() {
        Msg.InitTicker();
        Msg.TickerEnabled = !Msg.TickerEnabled;

        if (Msg.TickerEnabled) {
            $show(Msg.Ticker);
        }
        else {
            $hide(Msg.Ticker);
        }
    },

    HideTicker: function() {
        Msg.InitTicker();
        $hide(Msg.Ticker);
        Msg.TickerEnabled = false;
    },

    ShowTicker: function() {
        Msg.InitTicker();
        $show(Msg.Ticker);
        Msg.TickerEnabled = true;
    },

    ShowMessage: function() {
        if (div_Messages.style.display.length == 0)
            div_Messages.style.display = "none";
        else
            div_Messages.style.display = "";
    },

    RefreshMessages: function() {
        setElementToAjaxLoading(div_Messages);

        JSON.GetMessages(Msg.RefreshMessagesPostBack);
    },

    RefreshMessagesPostBack: function(dict) {
        div_Messages.innerHTML = "";
        //div_Messages.style.display = "";
        if (dict && parseInt(dict.Total) > 0) {
            if (parseInt(dict.New) > 0) {
                a_Messages.innerHTML = "<b>Messages(" + dict.Total + ")</b>";
                //do i do a toast box?
            }
            else {
                a_Messages.innerHTML = "Messages(" + dict.Total + ")";
            }

            Msg.InitTicker();
            Msg.TickerString = "";
            //div_Messages.style.display = "";
            var ul = document.createElement("ul");
            for (var i = 0; i < dict.Messages.length; i++) {
                var MsgItem = dict.Messages[i];

                var e = document.createElement("li");
                e.className = "message";
                e.innerHTML = MsgItem.Description;
                ul.appendChild(e);

                if (MsgItem.inTicker) {
                    Msg.TickerString += MsgItem.Title + ": " + MsgItem.TickerMessage + "&nbsp;&nbsp;|&nbsp;&nbsp;";
                }
            }

            if (Msg.TickerString.length > 0 && Msg.TickerEnabled) {
                $('TickerMarquee').innerHTML = Msg.TickerString;
                $show(Msg.Ticker);
                Msg.TickerEnabled = true;
            }
            else {
                $('TickerMarquee').innerHTML = "<hr />";
                $hide(Msg.Ticker);
                Msg.TickerEnabled = false;
            }

            var HTML = "<table border='0'><tr valign='" + (is_ie ? "top" : "middle") + "'><td><h4>Messages</h4></td><td>" +
                        "<img title='Refresh Messages' onclick='Msg.RefreshMessages()' border='0' src='" + PageTheme + "/refresh.gif' />&nbsp;" +
                        "<img title='Close Message Window' onclick='Msg.ShowMessage()' border='0' src='" + PageTheme + "/close.gif' />" +
                     "</td></tr></table><ul>" + ul.innerHTML + "</ul>";

            div_Messages.innerHTML = HTML;

            div_Messages.appendChild(document.createTextNode(" "));
            var a = document.createElement("a");
            a.innerHTML = "Toggle Ticker";
            a.onclick = Msg.ToggleTicker;
            a.href = "javascript:void(0)";
            div_Messages.appendChild(a);
        }
        else {
            //div_Messages.style.display = "none";
            a_Messages.innerHTML = "Messages(0)";
            div_Messages.innerHTML = "You have no messages";
            div_Messages.style.display = "none";
        }
    }
}

function PageFirstLoad()
{
   //set interval is in milliseconds
   //i.e 5 minutes = 5*60*1000
   setTimeout(function(){

   div_Widgets = $get("widgets");
   div_TabList = $get("tablist");
   div_NoWidgets = $get('div_nowidget');
   div_Messages = $get('div_Messages');
   div_tabHolder = $get('tabHolder');
   div_tabLastElement = $get('tabLastElement');
   a_Messages = $get('a_Messages');
   div_WidgetProgress = $get("progress");
   setElementToAjaxLoading(div_WidgetProgress);

   var today = new Date();
   WriteHistory("PageFirstLoad @ " + today);

   Menu_AddTarget('banner_search', 'banner_search_menu');
   var banner_search_txt = $('banner_search_txt');
   if (is_ie)
      banner_search_txt.onkeypress = function(){if(event.keyCode==13){Search_OnCRLF();event.keyCode=0;return false;}};
   else
      banner_search_txt.onkeypress = function(event){if(event.keyCode==13){Search_OnCRLF();return false;}};

   UpdateSearchEngine(1);
   Msg.RefreshMessages();
   setInterval("Msg.RefreshMessages()", 300000);
   Menu = new _menu();
   myTabs.FirstLoad();
   //ShowPageHint("Click <a href='javascript:void(0)' onclick='Menu.Show(1)'>here</a> to add content to this tab", 1);
   ShowPageHint("",0);
   
   //scrolling tab javascripts
   tab_width = div_TabList.clientWidth;

   if (window)
      window.onresize = resize;
      
   if (document.body)
      document.body.onresize = resize;

   resize();   

   var Scripts = document.getElementsByTagName("script");
   for(var i=0;i<Scripts.length;i++)
   {
      if (Scripts[i].src)
      {
         installScriptSrc_UpdateDict(Scripts[i]);
      }
   }
   
   RsaSecurityHelper.getSessionKey();
      
	}, 0);
}


function resize()
{
   myTabs.onTabChange();
}


Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(PageFirstLoad)